package callback;

import thread.ThreadUtils;
import threadpool.ThreadPoolFactory;

import java.util.List;
import java.util.concurrent.ExecutorService;

public class Student implements CallBack {

    private List<String> tasks;

    private final Teacher teacher;

    public Student(Teacher teacher) {
        this.teacher = teacher;
    }

    public void requireHomework(final String question) {
        ExecutorService threadPool = ThreadPoolFactory.getThreadPool();
        // 异步回调
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                teacher.assignHomework(Student.this, question);
            }
        });
        playGame();
        threadPool.shutdown();
        receiveTasks();
    }

    @Override
    public void setTasks(List<String> tasks) {
        System.out.println("学生请老师布置作业，今日作业为：" + tasks);
        this.tasks = tasks;
    }

    public void receiveTasks() {
        System.out.println("天呐，作业好多。作业：" + tasks);
    }

    private void playGame() {
        System.out.println("老师还没布置好作业，先打会游戏");
        ThreadUtils.sleep(2000);
    }
}
